package com.swgk.core.util;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;

import androidx.annotation.Nullable;

import com.swgk.core.base.model.entity.VideoBean;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static String showPrice(String s) {
        if ("0".equals(s) || TextUtils.isEmpty(s)) {
            s = "0.00";
        } else {
            Double price = Double.parseDouble(s);
            s = new DecimalFormat("0.00").format(price);
        }
        return s;
    }

    /**
     * @return
     */
    public static String formatUploadPrice(String s) {
        if ("0".equals(s) || TextUtils.isEmpty(s)) {
            s = "0.00";
        } else {
            Double price = Double.parseDouble(s);
            s = new DecimalFormat("0.00").format(price);
        }
        return s;
    }

    /***
     * 手机号部分数字转换含*符号方式一
     * @param name
     * @return
     */
    public static String getPhoneToName(String name) {
        if (TextUtils.isEmpty(name)) return "";
        if (name.length() < 11) {
            return name;
        }
        StringBuilder stringBuilder = new StringBuilder(name);
        stringBuilder.replace(3, 7, "****");
        return stringBuilder.toString();
    }

    /**
     * 只显示第一个汉字，其他隐藏为2个星号<例子：李**>
     *
     * @param fullName
     * @param index    1 为第index位
     * @return
     */
    public static String left(String fullName, int index) {
        if (StringUtils.isBlank(fullName)) {
            return "";
        }
        String name = StringUtils.left(fullName, index);
        return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
    }


    /**
     * 实名认证的真实姓名，只展示【最后一个字】，其余用【*】占位，示例：**明
     *
     * @param fullName
     * @return
     */

    public static String showRealName(String fullName) {
        if (StringUtils.isBlank(fullName)) {
            return "/";
        }

        if (fullName.length() > 1) {
            fullName = getStart(fullName.length() > 20 ? (fullName.length() - 7) : fullName.length() - 1) + fullName.substring(fullName.length() - 1, fullName.length());
        } else {
            fullName = "*";
        }

        return fullName;

    }

    public static String getStart(int size) {
        if (size == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size; i++) {
            sb.append("*");
        }
        return sb.toString();
    }

    /**
     * 脱敏公司名称
     */
    public static String showCompanyName(String name) {

        if (TextUtils.isEmpty(name)) return "/";

        if (name.length() < 12) {
            if (name.length() > 3) {
                name = name.substring(0, 3) + getStart(name.length() - 3);
            }
        } else {
            name = name.substring(0, 3) + getStart(name.length() - 5) + name.substring(name.length() - 2, name.length());
        }
        return name;

    }


    /***
     * 手机号部分数字转换含*符号方式二
     * @return
     */
    public static String gonePhone(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return "";
        }
        phoneNumber = phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        return phoneNumber;
    }

    /**
     * 显示距离保留两位小数
     */
    public static double fromatUpDistance(String num) {
        BigDecimal bg = new BigDecimal(Double.valueOf(num) / 1000);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /**
     * 处理已换成公里数的数据
     * 显示距离保留两位小数
     */
    public static double formatUpDistance(String num) {
        BigDecimal bg = new BigDecimal(Double.valueOf(num));
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /***
     * 身份证号部分数字转换含*符号方式一
     * @param idCard
     * @return
     */
    public static String getCoverIdcard(String idCard, int start, int end) {
        if (TextUtils.isEmpty(idCard) || idCard.length() < 18) return "";
        StringBuilder stringBuilder = new StringBuilder(idCard);
        stringBuilder.replace(start, end, "*********");
        return stringBuilder.toString();
    }

    /**
     * 隐藏姓名
     *
     * @param name
     * @return
     */
    public static String goneName(String name) {
        String reg = ".{1}";
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(name);
        int i = 0;
        while (m.find()) {
            i++;
            if (i == 1)
                continue;
            m.appendReplacement(sb, "*");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 去掉字符串首尾包含的指定字符
     *
     * @param source
     * @param chars
     * @return
     */

    public static String removeChar(String source, String chars) {
        if (TextUtils.isEmpty(source))
            return "";
        if (TextUtils.isEmpty(chars))
            return source;

        if (source.startsWith(chars) && source.length() >= 1)
            source = source.substring(1);

        if (source.endsWith(chars) && source.length() >= 2) {
            source = source.substring(0, source.length() - 1);
        }
        return source;
    }

    /**
     * 整数不保留小数点，小数保留
     */
    public static String formatDouble(double num) {
        if (num % 1.0 == 0) {
            return String.valueOf((long) num);
        }

        return String.valueOf(num);
    }

    public static boolean isEmpty(@Nullable String str) {
        return str == null || str.length() == 0 || "null".equals(str);
    }

    public static String getNotEmptyStr(@Nullable String str) {
        if (str == null || str.length() == 0 || "null".equals(str)) {
            return "";
        }
        return str;
    }


    public static String getNotEmptyStr2(@Nullable String str) {
        if (str == null || str.length() == 0 || "null".equals(str)) {
            return "/";
        }
        return str;
    }


    public static String getPrice(@Nullable String str, String unit) {
        if (str == null || str.length() == 0 || "null".equals(str)) {
            return "";
        }
        return price(Double.parseDouble(str)) + getNotEmptyStr(unit);
    }

    public static String getPrice2(@Nullable double p, String p1) {
        String price;
        if (p == 0) {
            price = p1;
        } else price = p + "";
        return getPrice(price, "");
    }

    public static String price(double price) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        return nf.format(price);
    }

    public static SpannableString getWebProfile(String s1, String s2) {
        String res = StringUtil.isEmpty(s2) ? s1 + "/" : s1 + s2;
        int length = s1.length();
        SpannableString spannableString = new SpannableString(res);
        spannableString.setSpan(new AbsoluteSizeSpan(14, true), 0, res.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#666666")), 0, length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), length, res.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static String formatListToString(List<String> str) {
        if (str == null || str.size() == 0) {
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        for (String s : str) {
            sb.append(s).append(",");
        }
        return sb.substring(0, sb.length() - 1);
    }

    /**
     * 判断字符串知否只包含数字
     */
    public static boolean isNumericAll(String str) {
        if (TextUtils.isEmpty(str))
            return false;
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }


    public static SpannableString setNoddleLine(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new StrikethroughSpan(), 0, s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setMoney(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(12, true), s.indexOf("."), s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
